<?php
class CenterAction extends StudyBaseAction
{
	/**
	 * 机构中心列表
	 */
	public function centerList()
	{
		$center = M('Center');
		if(isset($_POST['name']) && $_POST['name']!=null){ 
			$where['s_center.name'] = array('like',"%{$_POST['name']}%");
		}
		import('ORG.Util.Page');
		$count = $center->where($where)->count();// 查询满足要求的总记录数
		$page = new Page($count,10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $center->field('s_center.*,u.username,u.password')->where($where)->join("left JOIN s_user u on user_id = u.id")->limit($limit)->order("s_center.name desc")->select();
		$show = $page->show();
		$this->assign('page',$show);
		$this->assign('name',$_POST['name']);
		$this->assign('list',$list);
		$p = $_GET['p'];
		if(empty($p)){
			$p = 1;
		}
		$this->assign('p',$p);
		$this->display('Center:centerList');
	}
	/**
	 * 查看
	 */
	public function centerLook()
	{
		$form = M('Center');
		$where["s_center.id"] = $_GET['id'];
		$list = $form->field('s_center.*,u.username,u.password')->where($where)->join("left JOIN s_user u on user_id = u.id")->find();
		$game = M("Center_image");
		$center_image["center_id"] = $_GET['id'];
		$centerImage = $game->where($center_image)->select();
		$this->assign("list",$list);
		$this->assign("centerImage",$centerImage);
		$this->display();
	}
	/**
	 * 编辑页面
	 */
	public function centerEditView()
	{
		$form = M('Center');
		$where["s_center.id"] = $_GET['id'];
		$list = $form->field('s_center.*,u.username,u.password,u.id as u_id')->where($where)->join("left JOIN s_user u on user_id = u.id")->find();
		$game = M("Center_image");
		$center_image["center_id"] = $_GET['id'];
		$centerImage = $game->where($center_image)->select();
		$this->assign("list",$list);
		$this->assign("centerImage",$centerImage);
		$this->assign('p',$_GET['p']);
	    $this->display();
	}
	/**
	 * 删除
	 */
	public function centerDel()
	{
		$center = M("Center"); // 实例化Center对象
	  	$user = M('User'); // 实例化User对象
	  	$center_image = M('Center_image'); 
	  	$id['id']=$_GET['id'];
	  	$center_id['center_id']=$_GET['id'];
		$where['id'] = $center->where($id)->getField('user_id');//根据user_id得到用户
		$user->where($where)->delete(); // 删除用户
		$center_image->where($center_id)->delete(); // 删除图片
		$center->where($id)->delete(); // 删除机构
		//$_SESSION['message'] = '删除成功！';
	  	$p = $_GET['p'];
		$this->redirect('Center:centerList?p='.$p);
	}
	/**
	 * 新增页面
	 */
	public function centerAddView()
	{
	    $this->display();
	}
	/**
	 * 新增
	 */
	public function centerAdd()
	{
		//新增用户
		$user = M('User');
		$user->create();
		$save['username'] = $_POST['username'];
		$save['name'] = $_POST['username'];
		$save['password'] = $_POST['password'];
		$curr_time = date('Y-m-d H:i',time());
		$save['create_time']=$curr_time;
		$suc = $user->add($save);
		//新增机构
		$center = M('Center');
		$center->create();
		$center->create_time=$curr_time;
		$center->user_id=$suc;
		$id = $center->add();
		//导入图片上传类  
           import("ORG.Net.UploadFile");
           $upload = new UploadFile();// 实例化上传类
           $upload->maxSize  = 3145728 ;// 设置附件上传大小
           $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
           $upload->savePath =  './Public/Uploads/image/';// 设置附件上传目录
           //$upload->thumb=true;//是否开启图片文件缩略
		   //$upload->thumbMaxWidth='400,100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		   //$upload->thumbMaxHeight='400,100';
		   //$upload->thumbPrefix='s_,m_';  //缩略图文件前缀
		   //$upload->thumbSuffix='-s,-m';//后缀
		   //在数据库当中也存在一个文件名即可
			//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
            if(!$upload->upload()) {// 上传错误提示错误信息
            }else{// 上传成功 获取上传文件信息
                $info =  $upload->getUploadFileInfo();
            }
            // 保存表单数据 包括附件数据
            $game = M("Center_image");  
            $game->create();  
			for($i=0;$i<count($info);$i++){
		       $game->image = $info[$i]['savename'];   //等同与$img=array('img'=>$data[0]['savename']);
		       $game->center_id = $id;
		          $game->add();
		     }
		$this->redirect('Center:centerList');
	}
	/**
	 * 提交修改
	 */
	public function centerEdit()
	{
		//修改机构
		$center = M('Center');
		$save_center['long'] = $_POST['long'];
		$save_center['lat'] = $_POST['lat'];
		$save_center['name'] = $_POST['name'];
		$save_center['person'] = $_POST['person'];
		$save_center['phone'] = $_POST['phone'];
		$save_center['address'] = $_POST['address'];
		$save_center['authentication'] = $_POST['authentication'];
		$save_center['principal'] = $_POST['principal'];
		$save_center['editor'] = $_POST['editor'];
		$where_center['id'] = $_POST['id'];
		$center->where($where_center)->data($save_center)->save();
		$where['id'] = $center->where($where_center)->getField('user_id');//根据user_id得到用户
		//修改用户
		$user = M('User');
		//$save['username'] = $_POST['username'];
		//$save['name'] = $_POST['username'];
		$save['password'] = $_POST['password'];
		$where_user['id'] = $where['id'];
		$suc = $user->where($where)->data($save)->save();
		//导入图片上传类  
           import("ORG.Net.UploadFile");
           $upload = new UploadFile();// 实例化上传类
           $upload->maxSize  = 3145728 ;// 设置附件上传大小
           $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
           $upload->saveRule = uniqid;//这里的时间是根据上传的图片的多少来自动改变图片的名称的（并且时间都不同，所以上传的图片的名称就不会相同）
           $upload->savePath =  './Public/Uploads/image/';// 设置附件上传目录
           //$upload->thumb=true;//是否开启图片文件缩略
		   //$upload->thumbMaxWidth='400,100' ; //一字符串格式来传，如果希望有多个，那就在此处用，分割写上多个参数
		   //$upload->thumbMaxHeight='400,100';
		   //$upload->thumbPrefix='s_,m_';  //缩略图文件前缀
		   //$upload->thumbSuffix='-s,-m';//后缀
		   //在数据库当中也存在一个文件名即可
			//$upload->thumbRemoveOrigin=1;//如果生成缩略图，是否删除原图
            if(!$upload->upload()) {// 上传错误提示错误信息
            }else{// 上传成功 获取上传文件信息
                $info =  $upload->getUploadFileInfo();
            }
            // 保存表单数据 包括附件数据
            $game = M("Center_image");  
            //$game->create();  
			for($i=0;$i<count($info);$i++){
		       $game->image = $info[$i]['savename'];   //等同与$img=array('img'=>$data[0]['savename']);
		       $game->center_id = $_POST['id'];
		          $game->add();
		     }
		$this->redirect('Center:centerList');
	}
	/**
	 * 编辑时删除图片
	 */
	public function delImg()
	{
		$game = M("Center_image");
		$where['id']=$_POST['id'];
		$game->where($where)->delete(); // 删除
		$this->ajaxReturn("","JSON");
	}
	/**
	 * 验证用户名
	 */
	public function username()
	{
		$game = M("User");
		$where['username']=$_POST['username'];
		$username = $game->where($where)->select();
		$data = "error";
		if($username == null){
			$data = "success";
		}else{
			$data = "error";
		}
		$this->ajaxReturn($data,"JSON");
	}
	/**
	 * 验证用户名
	 */
	public function username_id()
	{
		$game = M("User");
		$where_id['id']=$_POST['u_id'];
		$where['username']=$_POST['username'];
		$name = $game->where($where_id)->getField('username');//根据user_id得到用户
		$username = $game->where($where)->select();
		$data = "error";
		if($username == null){
			$data = "success";
		}
		if($_POST['username'] == $name){
			$data = "success";
		}
		$this->ajaxReturn($data,"JSON");
	}
	/**
	 * 新增
	 */
	public function centerAddApply()
	{
		//新增用户
		$user = M('User');
		$user->create();
		$save['username'] = $_POST['phone'];
		$save['phone'] = $_POST['phone'];
		$save['name'] = $_POST['phone'];
		$save['type'] = '2';
		$save['password'] = '';
		$curr_time = date('Y-m-d H:i',time());
		$save['create_time']=$curr_time;
		$suc = $user->add($save);
		//新增机构
		$center = M('Center');
		$center->create();
		$center->create_time=$curr_time;
		$center->user_id=$suc;
		$id = $center->add();
		$this->redirect('/Index/index');
	}
}
?>